package com.kotlin.myapplication.mananger

import android.content.Context
import android.content.SharedPreferences
import com.kotlin.myapplication.app.App
import com.kotlin.myapplication.app.App.app.context
import com.kotlin.myapplication.mananger.APPSPManager.am.LEADPAGE
import com.kotlin.myapplication.mananger.APPSPManager.am.PREFS_NAME
import com.kotlin.myapplication.mananger.APPSPManager.am.ed
import com.kotlin.myapplication.mananger.APPSPManager.am.sp
import com.kotlin.myapplication.mananger.APPSPManager.am.userName
import com.kotlin.myapplication.utils.Logger

/**
 *@lil
 *@创建时间 2019/11/4 9:24
 *@描述
 */

class APPSPManager {

    object am {

        var sp: SharedPreferences? = null
        var context: Context? = null
        var ed: SharedPreferences.Editor? = null

        val PREFS_NAME: String = "miaokt"
        val userName: String = "userName"
        val loginState = "loginState"
        val LEADPAGE = "leadpage"
    }


    companion object {
        /**
         * SharedPreferences初始化，每次读写操作都必须
         */
        private fun initSharePreferences() {
            if (null == context) {
                context = App.getContext()!!
                if (null == context)
                    Logger.log("App.getContext() is null")
            }
            sp = context?.getSharedPreferences(PREFS_NAME,
                    Context.MODE_PRIVATE)
            ed = sp?.edit()
            ed?.commit()
        }

        fun setUserName(username: String?) {
            initSharePreferences()
            ed?.putString(userName, username)
            ed?.commit()
        }

        fun getUserName(): String? {
            initSharePreferences()
            return sp?.getString(userName, "")// 设置初始值
        }

        /**
         * 删除用户
         */
        fun delUserName() {
            initSharePreferences()
            ed?.remove(userName)
            ed?.commit()
        }

        fun setleadPage(leadpage: Boolean) {
            initSharePreferences()
            ed?.putBoolean(LEADPAGE, leadpage)
            ed?.commit()
        }

        fun getleadPage(): Boolean {
            initSharePreferences()
            return sp!!.getBoolean(LEADPAGE, false)// 设置初始值
        }

    }
}